<?php
namespace Home\Controller;
use Think\Controller;
class PersonalController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['uid']) || time() > $_SESSION['overtime'] || empty($_SESSION['overtime']) ) {

    		$this->redirect('Common/login');
    	}

        session('overtime',(time()+60*60*C('overtime')));

        
    }



    /**
     * 修改密码页面
     */
    public function personal(){
        $this->display();

    }


    /**
     * [ctrl_changePwd 修改密码]
     * @return [type] [description]
     */
    public function ctrl_changePwd(){
        if(IS_AJAX){
            $uid = $_REQUEST['uid'];
            $oldpwd = trim($_REQUEST['oldpwd']);
            $newpwd = trim($_REQUEST['newpwd']);
            $confirm_newpwd = trim($_REQUEST['confirm_newpwd']);

            $m_user = M('User');

            $where['id'] = $uid;
            $userInfo = $m_user->where($where)->find();

            if (empty($oldpwd) || empty($newpwd) || empty($confirm_newpwd) ) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '信息不完整，请重新填写'
                ));
            }else{
                if ($newpwd != $confirm_newpwd ) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '两次新密码不一致'
                    ));
                }else{
                    if (strlen($newpwd) < 6 || strlen($confirm_newpwd) < 6) {
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '新密码长度过短，建议长于>=6'
                        ));
                    }else{
                        if ($newpwd == $oldpwd) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '新密码与原密码相同，请重新输入'
                            ));
                        }else{
                            if ($userInfo['password'] != md5($oldpwd)) {
                                $this->ajaxReturn(array(
                                    'code'  =>0,
                                    'msg' => '原密码错误，请重新输入'
                                ));
                            }else{
                                $save['password'] = md5($newpwd);
                                $res = $m_user->where($where)->save($save);
                                if ($res) {
                                    $this->ajaxReturn(array(
                                        'code'  =>1,
                                        'msg' => '修改密码成功'
                                    ));
                                }else{
                                    $this->ajaxReturn(array(
                                        'code'  =>0,
                                        'msg' => '修改密码失败'
                                    ));
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [ctrl_changeSafe 修改交易密码]
     * @return [type] [description]
     */
    public function ctrl_changeSafe(){
        if (IS_AJAX) {
            $uid = $_REQUEST['uid'];
            $oldsafe = trim($_REQUEST['oldsafe']);
            $newsafe = trim($_REQUEST['newsafe']);
            $confirm_newsafe = trim($_REQUEST['confirm_newsafe']);

            $m_user = M('User');

            $where['id'] = $uid;
            $userInfo = $m_user->where($where)->find();

            if (empty($oldsafe) || empty($newsafe) || empty($confirm_newsafe) ) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '修改交易密码信息不完整，请重新填写'
                ));
            }else{
                if ($newsafe != $confirm_newsafe ) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '两次新交易密码不一致'
                    ));
                }else{
                    if (strlen($newsafe) < 6 || strlen($confirm_newsafe) < 6) {
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '新交易密码长度过短，建议长于>=6'
                        ));
                    }else{
                        if ($newsafe == $oldsafe) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '新交易密码与原交易密码相同，请重新输入'
                            ));
                        }else{
                            if ($userInfo['safe_password'] != md5($oldsafe)) {
                                $this->ajaxReturn(array(
                                    'code'  =>0,
                                    'msg' => '原交易密码错误，请重新输入'
                                ));
                            }else{
                                $save['safe_password'] = md5($newsafe);
                                $res = $m_user->where($where)->save($save);
                                if ($res) {
                                    $this->ajaxReturn(array(
                                        'code'  =>1,
                                        'msg' => '修改交易密码成功'
                                    ));
                                }else{
                                    $this->ajaxReturn(array(
                                        'code'  =>0,
                                        'msg' => '修改交易密码失败'
                                    ));
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [info 个人信息]
     * @return [type] [description]
     */
    public function info(){
        Vendor('phpqrcode.phpqrcode');
        $uid =$_SESSION['uid'];
        $m_user = M('User');

        $where['id'] = $uid;
        $userInfo = $m_user->where($where)->find();

        if(filter_var($_SERVER['SERVER_NAME'], FILTER_VALIDATE_IP)) {
            $promote = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].'/index.php/Common/promote?id='.$userInfo['id'];
        }else {
        // echo '不是IP地址';
            $promote = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].'/index.php/Common/promote?id='.$userInfo['id'];
        }

        $QRcode = new \QRcode ();
        ob_start();
        $QRcode::png($promote, "Uploads/qrcode/" . $uid . ".png");
        ob_end_clean();

        $qrcode = "/Uploads/qrcode/" . $uid . ".png";

        
        $this->assign('promote',$promote);
        $this->assign('qrcode',$qrcode);
        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /**
     * [ctrl_changeInfo 修改个人资料]
     * @return [type] [description]
     */
    public function ctrl_changeInfo(){
        if (IS_AJAX) {
            // print_r($_REQUEST);die();
            $uid = $_SESSION['uid'];
            $truename = trim($_REQUEST['truename']);
            $phone= trim($_REQUEST['phone']);
            $bankname = trim($_REQUEST['bankname']);
            $bankcard = trim($_REQUEST['bankcard']);
            $alipay_url = trim($_REQUEST['alipay_img_url']);
            $wx_url = trim($_REQUEST['wx_img_url']);

            $m_user = M('User');

            if (empty($bankcard) && empty($alipay_url) && empty($wx_url)) {
                $this->ajaxReturn(array(
                    'code'  =>0,
                    'msg' => '网银，支付宝，微信至少填写一项'
                ));
            }else{
                if (empty($bankcard) && !empty($bankname)) {
                    $this->ajaxReturn(array(
                        'code'  =>0,
                        'msg' => '网银资料不能为空'
                    ));
                }else{
                    if (!empty($bankcard) && empty($bankname)) {
                        $this->ajaxReturn(array(
                            'code'  =>0,
                            'msg' => '开户行不能为空'
                        ));
                    }else{
                        if (empty($truename) || empty($phone)) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '姓名、手机号或不能为空'
                            ));
                        }else{
                            if (!isMobile($phone)) {
                                $this->ajaxReturn(array(
                                    'code'=>0,
                                    'msg' => '手机格式不正确'
                                ));
                            }else{
                                $save['truename'] = $truename;
                                $save['phone'] = $phone;
                                $save['bankname'] = $bankname;
                                $save['bankcard'] = $bankcard;
                                $save['alipay_url'] = $alipay_url;
                                $save['wx_url'] = $wx_url;

                                $where['id'] = $uid;
                                $res = $m_user->where($where)->save($save);
                                if ($res) {
                                    $this->ajaxReturn(array(
                                        'code'=>1,
                                        'msg' => '保存成功'
                                    ));
                                }else{
                                    $this->ajaxReturn(array(
                                        'code'=>0,
                                        'msg' => '保存失败'
                                    ));
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [ctrl_activate 激活账号]
     * @return [type] [description]
     */
    public function ctrl_activate(){
        if (IS_AJAX) {
            $uid = $_REQUEST['uid'];
            if (empty($uid)) {
                $uid = $_SESSION['uid'];
            }

            $m_user = M('User');
            $reduceScore = C('activateScore');//扣除中心积分
            $registerScore = C('registerScore');//激活赠送任务积分

            $where_user['id'] = $uid;
            $userInfo = $m_user->where($where_user)->find();
            if ($userInfo['score_center'] < $reduceScore) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '中心积分小于'.$reduceScore.'，无法激活账号'
                ));
            }else{
                //添加中心积分记录、扣除中心积分、修改状态为已激活、赠送600任务积分
                //推荐人奖励动态秘钥
                
                $old_center = $userInfo['score_center'];
                $change_center = $reduceScore;
                $new_center = $old_center - $change_center;

                $old_task = $userInfo['score_task'];
                $change_task = $registerScore;
                $new_task = $old_task + $change_task;

                // 赠送任务秘钥
                // $old_keytask = $userInfo['key_task'];
                // $change_keytask = 5;
                // $new_keytask = $old_keytask + $change_keytask;
                
                //修改用户信息
                $save['score_center'] = $new_center;
                $save['status'] = 1;
                $save['score_task'] = $new_task;
                // $save['key_task'] = $new_keytask;
                $res = $m_user->where($where_user)->save($save);
                if ($res) {
                    //赠送推荐人动态秘钥
                    if (!empty($userInfo['top_id'])) {
                        givingDynamicKey($userInfo['top_id'],$userInfo['account']);
                    }
                    //添加中心积分记录
                    addCenterTable($userInfo['id'],$old_center,$change_center,$new_center,'激活账号扣除'.$change_center,2);
                    //添加任务积分记录
                    addTaskTable($userInfo['id'],$old_task,$change_task,$new_task,'激活账号赠送'.$change_task);
                    //添加任务秘钥记录
                    // addKeyTaskTable($userInfo['id'],$old_keytask,$change_keytask,$new_keytask,'激活账号赠送'.$change_keytask);
                    
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '激活成功'
                    ));
                    
                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '激活账号失败，请重新激活'
                    ));
                }
            }

        }else{
            $this->redirect('Common/BadRequest');
        }
    }


    /**
     * [ctrl_applyTask 申请任务执行函数]
     * @return [type] [description]
     */
    public function ctrl_applyTask(){
        if (IS_AJAX) {
            $uid = $_REQUEST['uid'];
            if (empty($uid)) {
                $uid = $_SESSION['uid'];
            }
            $m_user = M('User');
            $m_applytask = M('Applytask');

            $registerScore = C('registerScore');//赠送任务积分（申请任务积分）
            $giveScore = C('giveScore');//申请任务成功，赠送投注积分

            $where_user['id'] = $uid;
            $userInfo = $m_user->where($where_user)->find();
            if ($userInfo['status'] == 2) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '账号已被冻结'
                ));
            }else{
                if ($userInfo['status'] == 0) {
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '账号未激活'
                    ));
                }else{
                    if ($userInfo['key_task'] < 5) {
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '任务秘钥小于5'
                        ));
                    }else{
                        if ($userInfo['score_task'] < $registerScore) {
                            $this->ajaxReturn(array(
                                'code'=>0,
                                'msg' => '任务积分小于'.$registerScore
                            ));
                        }else{
                            if (empty($userInfo['phone'])) {
                                $this->ajaxReturn(array(
                                    'code'=>0,
                                    'msg' => '手机号信息不能为空'
                                ));
                            }else{
                                if (empty($userInfo['truename'])) {
                                    $this->ajaxReturn(array(
                                        'code'=>0,
                                        'msg' => '真实姓名不能为空'
                                    ));
                                }else{
                                    if (empty($userInfo['bankcard']) && empty($userInfo['wx_url']) && empty($userInfo['alipay_url']) ) {
                                        $this->ajaxReturn(array(
                                            'code'=>0,
                                            'msg' => '支付宝、微信、网银信息至少填写一项'
                                        ));
                                    }else{
                                        //是否存在正在进行中的任务
                                        $where_applytask['user_id'] = $uid;
                                        $where_applytask['status'] = 0;
                                        $hadTask = $m_applytask->where($where_applytask)->find();
                                        if ($hadTask['id']) {
                                            $this->ajaxReturn(array(
                                                'code'=>0,
                                                'msg' => '你有任务进行中，无法再次申请'
                                            ));
                                        }else{
                                            // 当天是否已经赢过一次
                                            $starttime = strtotime(date("Y-m-d 00:00:00"));
                                            $endtime = strtotime(date("Y-m-d 23:59:59"));

                                            $where_win['user_id'] = $uid;
                                            $where_win['winlose'] = 1;
                                            $where_win['createtime'] = array('between',array($starttime,$endtime));
                                            $winTask = $m_applytask->where($where_win)->find();
                                            if ($winTask['id']) {
                                                $this->ajaxReturn(array(
                                                    'code'=>0,
                                                    'msg' => '今天任务已完成，无法再次申请任务'
                                                ));
                                            }else{
                                                // 是否是10点后申请
                                                $tentime = strtotime(date("Y-m-d 12:00:00"));
                                                $nowtime = time();
                                                if ($nowtime < $tentime) {
                                                    $this->ajaxReturn(array(
                                                        'code'=>0,
                                                        'msg' => '暂未开放申请，请10点后再来'
                                                    ));
                                                }else{
                                                    //插入数据
                                                    $add['user_id'] = $uid;
                                                    $add['num'] = $registerScore + $giveScore;
                                                    $add['status'] = 0;
                                                    $add['createtime'] = time();

                                                    $res = $m_applytask->add($add);
                                                    if ($res) {
                                                        //修改用户数据：任务积分扣除600、任务秘钥扣除1、添加任务积分变动记录、添加任务秘钥变动记录
                                                        $old_task = $userInfo['score_task'];
                                                        $change_task = $registerScore;
                                                        $new_task = $old_task - $change_task;

                                                        $old_keytask = $userInfo['key_task'];
                                                        $change_keytask = 5;
                                                        $new_keytask = $old_keytask - $change_keytask;

                                                        $save['score_task'] = $new_task;
                                                        $save['key_task'] = $new_keytask;
                                                        $ressave = $m_user->where($where_user)->save($save);
                                                        if ($ressave) {
                                                            //添加任务积分记录
                                                            addTaskTable($userInfo['id'],$old_task,$change_task,$new_task,'申请任务扣除'.$change_task,2);
                                                            //添加任务秘钥记录
                                                            addKeyTaskTable($userInfo['id'],$old_keytask,$change_keytask,$new_keytask,'申请任务扣除'.$change_keytask,2);
                                                        }

                                                        $this->ajaxReturn(array(
                                                            'code'=>1,
                                                            'msg' => '申请成功',
                                                            'data'=>$change_keytask
                                                        ));
                                                    }else{
                                                        $this->ajaxReturn(array(
                                                            'code'=>0,
                                                            'msg' => '申请任务失败'
                                                        ));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }

}